// const { VertexBuffer } = require("XrFrame/kanata/lib/index")

// 在Page对象顶部添加字段映射常量
const FIELD_MAPPING = {
  education: "education background",
  work_experience: "work experience",
  project_experience: "project experience",
  certifications: "skill certificate",
  awards: "honor certificate",
  additional_info: "other info",
  position:"intent position",
  research_experience:"research experience",
  academic_achievements:"academic achievements",
  internship_experience:"internship experience",
  personal_statement:"personal statement"
};

Page({
  data: {
    fonts: ['系统默认',
      '苹方-简',
      '思源黑体',
      'Helvetica',
      'Times New Roman',
      'Courier New'], // 使用CSS有效字体名称
    sizes: [20, 24, 28, 32, 36, 42, 48, 56, 64], // 字号(rpx单位)
    font: 1,
    size: 2,
    bold: false,
    align: 'left',
    
    // 内容
    currentField: '',  // 新增当前编辑字段标识
    content: ''
  },

  // 在onLoad中添加数据初始化逻辑
  onLoad(options) {
      const app = getApp();
      // 初始化完整简历数据结构
      const defaultResume = {
          "type": "job",
          "education background": "",
          "research experience": "",
          "academic achievements": "",
          "internship experience": "",
          "work experience": "",
          "project experience": "",
          "skill certificate": "",
          "honor certificate": "",
          "other info": "",
          "intent position": "",
          "personal statement": "",
      };
      
      if (options.field) {
          // 合并已有数据
          const currentResume = app.globalData.resumeData || defaultResume;
          const decodedField = decodeURIComponent(options.field);
          
          this.setData({
              currentField: decodedField,
              content: currentResume[FIELD_MAPPING[decodedField]] || ""
          });
  
          // 更新全局数据保持结构完整
          app.globalData.resumeData = { ...defaultResume, ...currentResume };
      }
  },

  // 新增获取字段标签方法
  getFieldLabel(field) {
    const fieldMap = {
      position: '求职岗位',
      education: '教育背景',
      work_experience: '工作经历',
      project_experience: '项目经验',
      certifications: '技能证书',
      awards: '荣誉奖励',
      additional_info: '其他信息'
    }
    return fieldMap[field] || '未知字段'
  },

  saveResume() {
    const { content, currentField } = this.data
    if (!content.trim()) {
      wx.showToast({ title: '内容不能为空', icon: 'none' })
      return
    }
  
    // 转换字段名为后端需要的格式
    const backendField = FIELD_MAPPING[currentField] || currentField;
    
    // 获取并更新全局数据
    const app = getApp();
    const fullResume = app.globalData.resumeData || {};
    
    // 更新对应字段
    fullResume[backendField] = content;
    app.globalData.resumeData = fullResume;
    
    wx.showToast({ title: '保存成功' })
    setTimeout(() => wx.navigateBack(), 1500)
  },
  // 字体选择
  setFont(e) {
    this.setData({ font: e.detail.value })
  },

  // 字号选择
  setSize(e) {
    this.setData({ size: e.detail.value })
  },

  // 粗体切换
  toggleBold() {
    this.setData({ bold: !this.data.bold })
  },


  // 对齐方式
  setAlign(e) {
    this.setData({ align: e.currentTarget.dataset.align })
  },

  // 输入处理
  onInput(e) {
    this.setData({ content: e.detail.value })
  },
  goBack() {
    wx.navigateBack()  // 已经正确实现返回功能
  }
})